/*******************************************************************************
* Copyright (c) 2010, 2018 西安秦晔信息科技有限公司
* Licensed under the Apache License, Version 2.0 (the "License");
*    you may not use this file except in compliance with the License.
*    You may obtain a copy of the License at
*
*       http://www.apache.org/licenses/LICENSE-2.0
*
*    Unless required by applicable law or agreed to in writing, software
*    distributed under the License is distributed on an "AS IS" BASIS,
*    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*    See the License for the specific language governing permissions and
*    limitations under the License.
*******************************************************************************/
package com.qinyeit.serviceapp.service.members.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.qinyeit.serviceapp.entity.*;
import com.qinyeit.serviceapp.exception.BusinessRunTimeException;
import com.qinyeit.serviceapp.repository.members.IntegralRuleBranchRepository;
import com.qinyeit.serviceapp.service.branchs.BranchDataReleasedServiceImpl;
import com.qinyeit.serviceapp.service.organization.BranchService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.qinyetech.springstage.core.service.BaseService;
import com.qinyeit.serviceapp.repository.members.IntegralRuleRepository;
import com.qinyeit.serviceapp.service.members.IntegralRuleService;

import java.util.List;

/** 
 * ClassName: IntegralRuleServiceImpl <br/> 
 * Function: 积分规则 业务实现 <br/> 
 * date: 2018-12-04 10:49:12 <br/> 
 * 
 * @author liuwenjin
 * @version 1.0
 * @since JDK 1.8
 */
@Service
@Transactional
public class IntegralRuleServiceImpl extends BaseService<IntegralRule, Long> implements IntegralRuleService {


	@Autowired
	private IntegralRuleRepository integralRuleRepository;
	@Autowired
	private IntegralRuleBranchRepository integralRuleBranchRepository;
	@Autowired
	private BranchService branchService;
	@Autowired
	private BranchDataReleasedServiceImpl branchDataReleasedService;

	@Override
	public void allocationBranchSave(JSONObject data) {
		JSONArray ids = data.getJSONArray("ids");
		JSONArray branchIds = data.getJSONArray("branchs");
		if(CollectionUtils.isEmpty(ids)){
			throw new BusinessRunTimeException("请选择积分规则",10001);
		}

		if(CollectionUtils.isEmpty(branchIds)){
			throw new BusinessRunTimeException("分店不能为空",10003);
		}
		IntegralRule integralRule = null;
		Branch branch = null;
		IntegralRuleBranch integralRuleBranch = null;
		List<IntegralRuleBranch> integralRuleBranches = Lists.newArrayList();
		List<IntegralRule> integralRuleList = Lists.newArrayList();
		for (int i = 0; i < ids.size(); i++) {
			integralRule = this.findById(ids.getLong(i));
			if(integralRule==null){
				throw new BusinessRunTimeException("选择的积分规则不存在",10004);
			}
			integralRuleList.add(integralRule);
			//先删除以前的分店信息
			integralRuleRepository.deleteBranchs(integralRule.getId());

			for (int n = 0;n < branchIds.size(); n++) {
				branch = branchService.findById(branchIds.getLong(n));
				if(branch==null){
					throw new BusinessRunTimeException("门店不存在",10005);
				}
				integralRuleBranch = new IntegralRuleBranch();
				integralRuleBranch.setBranchId(branchIds.getLong(n));
				integralRuleBranch.setIntegralRuleId(integralRule.getId());
				integralRuleBranch.setDistrictId(branch.getDistrictId());
				integralRuleBranch.setMerchantGroupId(integralRule.getMerchantGroupId());
				integralRuleBranches.add(integralRuleBranch);
			}
		}
		integralRuleBranchRepository.saveAll(integralRuleBranches);

		List<Long> bIDs = branchIds.toJavaList(Long.class);
		branchDataReleasedService.updateIntegeralRule( bIDs, integralRuleList);
	}

}